<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/quest.jsp"%>
<%@ include file="/common/message.jsp"%>
<%!
	// 回复
	String reply(Integer uid, Input input) {
		Message msg = new Message(0);
		Integer questId = input.getInt("id", 0);
		String content = filterIllegalCharacter(input.getDesc("content", ""));
		Integer hospital = input.getInt("hospital", -1);
		Integer parentId = input.getInt("parent_id", null);
		Integer atUid = input.getInt("at_uid", null);
		if (content.length() < 3) {
			return new Message(30041, "回复内容不能少于3个字哦").toJson();
		}
		if (content.length() > 2000) {
			return new Message(30041, "回复内容不能大于2000个字哦").toJson();
		}
		List<Quest> questList = QBuilder.db("basedb").queryHql("from Quest where id=" + questId);
		if (questList.size() == 0) {
			return new Message(30041, "该条记录不存在").toJson();
		}
		Quest quest = questList.get(0);
		com.tianxiabuyi.model.pojo.Comment comment = new com.tianxiabuyi.model.pojo.Comment();
		comment.setHospital(hospital);
		comment.setUid(uid);
		comment.setSrcId(questId);
		comment.setContent(content);
		comment.setCategory(1);
		comment.setParentId(parentId);
		comment.setAtUid(atUid);
		String imgs = input.getDesc("imgs", "");
		comment.setImgs(JSONUtil.toJson(stringToList(imgs)));
		DBTools.db("basedb").addItem(comment);
		String title = quest.getTitle();
		if (title.equals("发布圈子") || title.equals("药师服务")) {
			title = quest.getContent();
		}
		if (title.length() > 10) {
			title = title.substring(0, 10) + "...";
		}
		Integer questUid = quest.getUid();
		User commentUser = userByUid(uid);
		String tips = String.format("%s参与并回复了帖子“%s”，请您查看", new Object[]{getDisplayName(commentUser), title});
		List<Integer> uids = QBuilder.db("basedb").select("distinct uid").from("Comment as c").where(new Where("c.srcId=", questId).and("c.hospital=", hospital).and("c.category=", 1).and("c.status=", STATUS_ENABLE)).query().getResultObj();
		// 自己评论不提醒自己
		uids.add(questUid);
		Iterator<Integer> iter = uids.iterator();
		while(iter.hasNext()) {
			Integer itUid = iter.next();
			if (uid.equals(itUid))
				iter.remove();
		}
		if (quest.getTitle().equals("药师服务"))
			createMsg(uids, hospital, "药师回复提醒", "药师服务提醒", tips, QUESTINFO, questId + "");
		else
			createMsg(uids, hospital, "圈子回复提醒", "圈子提醒", tips, QUESTINFO, questId + "");
		return show(input);
	}

%>
<%
	Tools.encryptPrint(out, reply(token.getUid(), input));
%>
